package com.bt.lms.ui.action.teaching.plan.query;

import com.alibaba.fastjson.JSONObject;
import com.bt.lms.entity.Major;
import com.bt.lms.entity.TeachingPlanBasicInfo;
import com.bt.lms.entity.page.Page;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.ui.action.teaching.plan.LTeachingPlanEditBtnAction;
import com.bt.lms.ui.action.teaching.plan.LTeachingPlanViewDetailsAction;
import com.bt.lms.ui.vo.teachingplan.TeachingPlanVo;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 李舒淇
 */
public class UnbindPackage {

    public Integer current;

    /**
     * 5、解包
     *
     * @param acceptBuffer 服务端发送过来的数组
     * @param
     */
    public Integer unbindPackage(byte[] acceptBuffer, TableView tableView) {

        current = 1;



        //解包
        RespMsg respMsg = RespMsg.toRespMsg(acceptBuffer);
        String data = respMsg.getData();//获取数据
        Page page = JSONObject.parseObject(data, Page.class);//处理数据
        Integer current = page.getCurrent();
        List records = page.getRecords();//分页里的集合
        List<TeachingPlanBasicInfo> teachingPlanBasicInfos = new ArrayList<>();//新的集合

        for (int i = 0; i < records.size(); i++) {

            Object teachingInfo = records.get(i);
            String teachingInfoStr = teachingInfo.toString();
            TeachingPlanBasicInfo teachingPlanBasicInfo = JSONObject.parseObject(teachingInfoStr, TeachingPlanBasicInfo.class);
            teachingPlanBasicInfos.add(teachingPlanBasicInfo);

        }

        //处理集合里的数据
        List<TeachingPlanVo> teachingPlanVos = new ArrayList<>();


        for (int i = 0; i < teachingPlanBasicInfos.size(); i++) {

            TeachingPlanBasicInfo teachingPlanBasicInfo = teachingPlanBasicInfos.get(i);

            Long id = teachingPlanBasicInfo.getID();//id

            String code = teachingPlanBasicInfo.getCode();//计划编号

            String name = teachingPlanBasicInfo.getName();//教学计划名称
            LTeachingPlanViewDetailsAction lTeachingPlanViewDetailsAction = new LTeachingPlanViewDetailsAction();


            Integer learningCenterI = teachingPlanBasicInfo.getLearningCenter();//所属的学习中心
            String learningCenter = null;
            switch (learningCenterI) {

                case 2:
                    learningCenter = "北京师范大学";
                    break;
                case 1:
                    learningCenter = "北京大学";
                    break;
            }

            Major majorInfo = teachingPlanBasicInfo.getMajorInfo();//所属专业


            Integer applyLevel = teachingPlanBasicInfo.getApplyLevel();//适用层次
            String applyLevelStr = null;
            switch (applyLevel) {
                case 1:
                    applyLevelStr = "高中";
                    break;
                case 2:
                    applyLevelStr = "大专";
                    break;
                case 3:
                    applyLevelStr = "本科";
                    break;
            }

            Integer lengthOfSchoolingText = teachingPlanBasicInfo.getLengthOfSchoolingText();//学制文本
            String lengthOfSchoolingTStr = lengthOfSchoolingText.toString();

            Integer lengthOfSchooling = teachingPlanBasicInfo.getLengthOfSchooling();//学制单位
            String lengthOfSchoolingStr = null;
            switch (lengthOfSchooling) {
                case 1:
                    lengthOfSchoolingStr = "年";
                    break;
                case 2:
                    lengthOfSchoolingStr = "月";
                    break;
                case 3:
                    lengthOfSchoolingStr = "学时";
                    break;
            }
            String lengthOfSchoolingT = lengthOfSchoolingTStr + lengthOfSchoolingStr;//学制

            Integer statusI = teachingPlanBasicInfo.getStatus();//状态
            String status = null;
            switch (statusI) {
                case 1:
                    status = "草稿";
                    break;
                case 2:
                    status = "已启用";
                    break;
                case 3:
                    status = "已完成";
                    break;
            }

            Integer relCourseQuantity = teachingPlanBasicInfo.getRelCourseQuantity();//关联课程数

            String currentTerm = teachingPlanBasicInfo.getCurrentTerm();//等前学期

            HBox operationHBox = new HBox();
            operationHBox.setSpacing(10);
            if (status.equals("草稿")) {

                Button enableBtn = new Button("启用");
                enableBtn.setCursor(Cursor.HAND);


                Button editBtn = new Button("编辑");
                editBtn.setCursor(Cursor.HAND);
                LTeachingPlanEditBtnAction teachingPlanEditBtnAction = new LTeachingPlanEditBtnAction(id);
                editBtn.setOnMouseClicked(teachingPlanEditBtnAction);


                Button deleteBtn = new Button("删除");
                deleteBtn.setCursor(Cursor.HAND);
                Button addCourseBtn = new Button("新增关联课程");
                addCourseBtn.setCursor(Cursor.HAND);
                operationHBox.getChildren().addAll(enableBtn, editBtn, deleteBtn, addCourseBtn);
            } else if (status.equals("已启用")) {
                Button confirmCompletionBtn = new Button("确认完成");
                confirmCompletionBtn.setCursor(Cursor.HAND);
                operationHBox.getChildren().add(confirmCompletionBtn);
            }
            CheckBox checkBox = new CheckBox();
            TeachingPlanVo teachingPlanVo = new TeachingPlanVo(checkBox, code, name, learningCenter, "", applyLevelStr, lengthOfSchoolingT, status, relCourseQuantity, currentTerm, operationHBox);
            teachingPlanVos.add(teachingPlanVo);
        }


        //将教学计划数据集合转换成Javafx组件集合
        ObservableList<TeachingPlanVo> teachingPlanBasicInfos1 = FXCollections.observableArrayList(teachingPlanVos);
        //把数据放入表格当中
        tableView.setItems(teachingPlanBasicInfos1);

        return current;

    }

}
